<template>
  <div>
    <input v-model="inputvalue" placeholder="输入关键字搜索" />
    <table class="my-table">
      <thead>
        <tr>
          <th>就诊日期</th>
          <th>医生姓名</th>
          <th>诊断结果</th>
          <th>处方信息</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in list" :key="item.date">
          <td>{{ item.date }}</td>
          <td>{{ item.doctor }}</td>
          <td>{{ item.diagnosis }}</td>
          <td>{{ item.prescription }}</td>
          <td @click="btn(item.doctor)">详情</td>
        </tr>
      </tbody>
    </table>
    <detail
      :list="list"
      v-if="flag"
      :flag.sync="flag"
      :dcname="dcname"
    ></detail>
  </div>
</template> 
<script>
import detail from './his/Detail.vue'
export default {
  /*  
  实现搜索框根据关键词 查询出对应的数据
  封装一个表格组件，组件里边人记录，根据父组件传递过来的数组展示
  封装一个详情组件
  点击详情的时候，可以打开弹窗组件，并且展示用户的信息
  点击X关闭弹窗 组件
  */
  name: '',
  components: { detail },
  data () {
    return {
      list: [
        {
          date: "2022-01-01",
          doctor: "张三",
          diagnosis: "感冒",
          prescription: "感冒药",
        },
        {
          date: "2022-02-01",
          doctor: "李四",
          diagnosis: "头疼",
          prescription: "止疼药",
        },
        {
          date: "2022-03-01",
          doctor: "王五",
          diagnosis: "腰痛",
          prescription: "止痛贴",
        }, {
          date: "2022-03-05",
          doctor: "王吴人",
          diagnosis: "腰痛",
          prescription: "止痛贴",
        },
      ],
      flag: false,
      newArr: [],
      inputvalue: '',
      dcname: ''
    }
  },
  created () {
    this.newArr = this.list
  },
  computed: {
  },
  methods: {
    btn (name) {
      this.flag = true
      // console.log(this.flag);
      // console.log(name);
      this.dcname = name
    }
  },
  watch: {
    inputvalue (newval) {
      console.log(newval);
      if (newval !== '') {
        this.list = this.newArr.filter((item) => item.doctor.includes(newval.trim()) || item.doctor === newval.trim())
      } else {
        console.log(11);
        console.log(this.newArr);
        this.list = this.newArr
      }
    }
  }
}
</script>
<style lang='less'  scoped>
.my-table {
  border-collapse: collapse;
  width: 100%;
}

.my-table td,
.my-table th {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.my-table th {
  background-color: #f2f2f2;
}
</style>
